﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/SystemManagers/SystemManagers.master"
    Inherits="System.Web.Mvc.ViewPage<Diploma.Models.OrderPractice>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Работа с приказом на практику
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div class="order">
        <%
            Html.RenderPartial("~/Views/SystemManagers/OrderPractice/OrderMenu.ascx",
                               (Model.IsVerify.HasValue && Model.IsVerify.Value));%>
        <div class="order_body">
            <div id="order-info" class="order-div-head">
                Информация о приказе
            </div>
            <div id="order-info-div">
            </div>
            <%
                if (Model.IsVerify.HasValue && !Model.IsVerify.Value)
                {%>
            <div id="order-add-student" class="order-div-head">
                Добавить студентов
            </div>
            <div id="order-add-student-div">
                <div id="order-levels">
                </div>
                <div id="order-groups">
                </div>
            </div>
            <%
                }%>
            <%
                if (Model.IsVerify.HasValue && Model.IsVerify.Value)
                {%>
            <div id="order-sub" class="order-div-head">
                Под приказы
            </div>
            <div id="order-sub-list">
            </div>
            <%
                }%>
            <div id="order-student" class="order-div-head">
                Студенты
            </div>
            <div id="order-student-div">
            </div>
        </div>
        <div style="clear: both;">
        </div>
    </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ScriptsContent" runat="server">
    <script type="text/javascript">
        var orderId = '<%:Model.Id.ToString()%>';

        $(document).ready(function() {
            LoadInfo();
            LoadLevelList();
            RefreshStudentList();
        });

        function LoadInfo() {
            var path = '<%:Url.RouteUrl("Default", new {controller = "OrderPractice", action = "LoadInfo"})%>';

            $.ajax({
                    url: path,
                    type: "POST",
                    async: false,
                    data: ({ orderId: orderId }),
                    success: function(response) {
                        $("#order-info-div").html(response);
                    }
                });
        }

        function LoadEditInfo() {
            var path = '<%:Url.RouteUrl("Default", new {controller = "OrderPractice", action = "LoadEditInfo"})%>';

            $.ajax({
                    url: path,
                    type: "POST",
                    async: false,
                    data: ({ orderId: orderId }),
                    success: function(response) {
                        $("#order-info-div").html(response);
                        $("#StopDate").datepicker($.datepicker.regional['ru']);
                        $("#StartDate").datepicker($.datepicker.regional['ru']);
                    }
                });
        }

        function RefreshStudentList() {
            var path = '<%:Url.RouteUrl("Default", new {controller = "OrderPractice", action = "RefreshStudentList"})%>';

            $.ajax({
                    url: path,
                    type: "POST",
                    async: false,
                    data: ({ orderId: orderId }),
                    success: function(data) {
                        $("#order-student-div").html(data);
                    }
                });
        }

        function SaveInfoChanges() {
            var name = $("#Name").val();
            var startDate = $("#StartDate").val();
            var endDate = $("#StopDate").val();

            var path = '<%:Url.RouteUrl("Default", new {controller = "OrderPractice", action = "SaveEditInfo"})%>';

            $.ajax({
                    url: path,
                    type: "POST",
                    async: false,
                    data: ({ orderId: orderId, orderName: name, startDate: startDate, endDate: endDate }),
                    success: function(response) {
                        if (response.status) {
                            LoadInfo();
                        }
                    }
                });
        }

        function LoadLevelList() {
            var path = '<%:Url.RouteUrl("Default", new {controller = "OrderPractice", action = "GetAllLevels"})%>';

            $.ajax({
                    url: path,
                    type: "POST",
                    async: false,
                    success: function(response) {
                        $("#order-levels").html(response);
                    }
                });
        }

        function LoadGroups() {
            var levelId = $("#AllLevels option:selected").val();
            var path = '<%:Url.RouteUrl("Default", new {controller = "OrderPractice", action = "GetGroups"})%>';

            $.ajax({
                    url: path,
                    type: "POST",
                    async: false,
                    data: ({ levelId: levelId }),
                    success: function(response) {
                        $("#order-groups").html(response);
                    }
                });
        }

        function AddStudentToOrder() {
            var groupId = $("#AllGroups option:selected").val();
            var path = '<%:Url.RouteUrl("Default",
                                           new {controller = "OrderPractice", action = "AttachStudentToOrder"})%>';

            $.ajax({
                    url: path,
                    type: "POST",
                    async: false,
                    data: ({ groupId: groupId, orderId: orderId }),
                    success: function(data) {
                        RefreshStudentList();
                    }
                });
        }

        function RemoveStudent(studentId) {
            var path = '<%:Url.RouteUrl("Default", new {controller = "OrderPractice", action = "RemoveUser"})%>';
            $.ajax({
                    url: path,
                    type: "POST",
                    async: false,
                    data: ({ orderId: orderId, studentId: studentId }),
                    success: function(response) {
                        RefreshStudentList();
                    }
                });
        }

        function SaveStateOrderPractive() {
            var path = '<%:Url.RouteUrl("Default", new {controller = "OrderPractice", action = "SaveState"})%>';
            $.ajax({
                    url: path,
                    type: "POST",
                    async: false,
                    data: ({ orderId: orderId }),
                    success: function(response) {
                        window.location.reload(true);
                    }
                });
        }

    </script>
    <%
        if (Model.IsVerify.HasValue && Model.IsVerify.Value)
        {%>
    <script type="text/javascript">
        $(document).ready(function() {
            LoadSubOrderList();

            $("#order-sub").click(function() {
                $("#order-sub-list").fadeToggle("slow");
            });
        });

        function CreateSubOrder() {
            $(".CreateSubOrder").show();
            RemoverContaintStudents();
        }

        function AddStudentToList(studentId) {
            var trString = "#student_item_" + studentId;
            var divString = "#student_" + studentId;
            if ($(divString).hasClass("addBottom")) {
                $(trString).attr("style", "background-color: #F8F8F8;");
                $(divString).attr("studentId", studentId);

                $(divString).removeClass("addBottom");
                $(divString).addClass("remBottom");
            } else {
                $(trString).removeAttr("style");
                $(divString).removeAttr("studentId");

                $(divString).removeClass("remBottom");
                $(divString).addClass("addBottom");
            }
        }

        function AddStudentsToSubOrder() {
            var students = "";
            var elements = $('[studentId*="-"]');
            elements.each(function() {
                var item = $(this).attr("studentId");
                students += item;
                students += ";";
            });
            if (students.length == 0) {
                alert("Вы не выбрали студентов для под приказа");
            } else {
                students = students.slice(0, -1);
                var path = '<%:Url.RouteUrl("Default", new {controller = "SubOrderPractice", action = "CreateSubOrder"})%>';

                $.ajax({
                        url: path,
                        type: "POST",
                        async: false,
                        data: ({ students: students, orderId: orderId }),
                        success: function(response) {
                            if (response.status) {
                                alert("Подприказ был создан");
                                window.location.reload(true);
                            } else {
                                alert("Ошибка. Свяжитесь с админитратором.");
                            }
                        },
                        error: function() {
                            alert("Ошибка. Свяжитесь с админитратором.");
                        }
                    });
            }
        }

        function LoadSubOrderList() {
            var path = '<%:Url.RouteUrl("Default", new {controller = "OrderPractice", action = "GetSubOrders"})%>';

            $.ajax({
                    url: path,
                    type: "POST",
                    async: false,
                    data: ({ orderId: orderId }),
                    success: function(response) {
                        $("#order-sub-list").html(response);
                    }
                });
        }

        function RemoverContaintStudents() {
            var path = '<%:Url.RouteUrl("Default",
                                           new {controller = "OrderPractice", action = "GetStudentListInSubOrders"})%>';

            $.ajax({
                    url: path,
                    type: "POST",
                    async: false,
                    data: ({ orderId: orderId }),
                    success: function(response) {
                        for (var i = 0; i < response.length; i++) {
                            $("#student_item_" + response[i]).remove();
                        }
                    }
                });
        }
    </script>
    <%
        }%>
</asp:Content>
